package com.hsnn.medstgmini.trading.pur.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.hsnn.medstgmini.base.dict.dao.impl.BaseHibernateDAO;
import com.hsnn.medstgmini.dao.HibernateSqlUtil;
import com.hsnn.medstgmini.trading.pur.dao.OrderNotConfirmDao;
import com.hsnn.medstgmini.trading.pur.model.OrderPackStatus;
import com.hsnn.medstgmini.trading.pur.model.PurOrderInfo;
import com.hsnn.medstgmini.trading.pur.model.PurOrderpack;
import com.hsnn.medstgmini.trading.pur.model.form.OrderPack;
import com.hsnn.medstgmini.trading.pur.model.form.OrderPackage;
import com.hsnn.medstgmini.util.DateUtil;
import com.hsnn.medstgmini.util.Pagination;



/**
 * @category 中心端查看订单明细
 * @author 王立志
 * @date 2015年7月9日
 */
	@Repository
	public class OrderNotConfirmDaoImpl extends BaseHibernateDAO implements OrderNotConfirmDao {
		
		@Resource(name = "sessionFactory")
		private SessionFactory sessionFactory;
		@Autowired
		private HibernateSqlUtil dao;
		
		
		/**
		 * 
		 * @category 获取审核未通过订单信息列表数据（医疗机构）
		 * @author 林洋
		 * @date 2016年4月10日 
		 * @param page
		 * @return
		 */
		public Pagination getNotPassOrderList(Pagination page) {
			StringBuilder sb = new StringBuilder();
			sb.append(" SELECT t.remarks,t.order_pack_id orderPackId,t.hosp_code hospCode,t.count, ");
			sb.append(" t.amount,t.is_urgent isUrgent, ");
			sb.append(" t.submit_datetime submitDatetime,t.del_comp_code delCompCode,t.order_status orderStatus, ");
			sb.append(" t.recp_addr recpAddr, t.recp_userphone recpUserphone, t.recp_user recpUser,t.del_comp_name delCompName ");
			sb.append(" from pur_orderpack t ");
			sb.append(" where t.hosp_code=:hospCode and t.order_status != :orderStatus and t.order_status=12 ");
			OrderPackage orderPackage = (OrderPackage) page.getForm();
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("orderStatus", OrderPackStatus.finishOrder.getKey());
			params.put("hospCode", orderPackage.getHospCode());
			
			if (StringUtils.isNotBlank(orderPackage.getOrderPackId())) {
				sb.append(" AND t.order_pack_id = :orderPackId ");
				params.put("orderPackId", orderPackage.getOrderPackId());
			}
			
			if (orderPackage.getStartDate() != null) {
				sb.append(" AND DATE_FORMAT(t.submit_datetime, '%Y-%m-%d') >= :submitDatetimeStr ");
				params.put("submitDatetimeStr", DateUtil.date(orderPackage.getStartDate()));
			}
			
			if (orderPackage.getEndDate() != null) {
				sb.append(" AND DATE_FORMAT(t.submit_datetime, '%Y-%m-%d') <= :submitDatetimeEnd ");
				params.put("submitDatetimeEnd", DateUtil.date(orderPackage.getEndDate()));
			}
			
			if(StringUtils.isNotBlank(orderPackage.getDelCompName())){
				sb.append(" and t.del_comp_name like :delCompName or t.del_comp_name_spel like :delCompName");
				params.put("delCompName", "%"+orderPackage.getDelCompName()+"%");
			}
			/*if (orderPackage.getOrderStatus() != null) {
				sb.append(" AND t.order_status = :orderStatusF ");
				params.put("orderStatusF", orderPackage.getOrderStatus());
			}*/
			sb.append(" order by t.submit_datetime desc ");
			return dao.getPageMapList(sb.toString(), params, page);
	}


		@Override
		public void delete(List<OrderPack> list) {
			if (null != list && list.size() > 0) {
				String sql = "DELETE from pur_orderpack where order_pack_id in (:orderPackId)";
				Map<String, Object> params = new HashMap<String, Object>();
				List<String> plist = new ArrayList<String>();
				for (int i = 0; i < list.size(); i++) {
					OrderPack form = list.get(i);
					plist.add(form.getOrderPackId());
				}
				params.put("orderPackId", plist);
				dao.executeSql(sql, params);
			}
			
		}


		@Override
		public void orderSubmit(List<OrderPack> list) {
			if (null != list && list.size() > 0) {
				String sql = "update pur_orderpack set order_status=1 where order_pack_id in (:orderPackId)";
				Map<String, Object> params = new HashMap<String, Object>();
				List<String> plist = new ArrayList<String>();
				for (int i = 0; i < list.size(); i++) {
					OrderPack form = list.get(i);
					plist.add(form.getOrderPackId());
				}
				params.put("orderPackId", plist);
				dao.executeSql(sql, params);
			}
			
			
		}


		@Override
		public void orderDelete(List<PurOrderInfo> list) {
			if (null != list && list.size() > 0) {
				String sql = "DELETE from pur_orderinfo where order_code in (:orderCode)";
				Map<String, Object> params = new HashMap<String, Object>();
				List<String> plist = new ArrayList<String>();
				for (int i = 0; i < list.size(); i++) {
					PurOrderInfo form = list.get(i);
					plist.add(form.getOrderCode());
				}
				params.put("orderCode", plist);
				dao.executeSql(sql, params);
			}
			
		}



		@Override
		public void orderPass(List<OrderPack> list) {
			if (null != list && list.size() > 0) {
				String sql = "update pur_orderpack set order_status=1 where order_pack_id in (:orderPackId)";
				Map<String, Object> params = new HashMap<String, Object>();
				List<String> plist = new ArrayList<String>();
				for (int i = 0; i < list.size(); i++) {
					OrderPack form = list.get(i);
					plist.add(form.getOrderPackId());
				}
				params.put("orderPackId", plist);
				dao.executeSql(sql, params);
			}
			
			
		}

	}
